package com.qf.web;


import com.qf.model.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 个人信息页面的Servlet
 * Personal Information
 * */
@WebServlet("/PformationServlet")
public class PformationServlet extends BaseServlet{
    //new一个业务层对象
     UserService userService = new UserServiceImpl();

     /**
      * 点击【头像】调用的方法
      * select
      * */
    public void select(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        //获取参数
//        String name = req.getParameter("name");//姓名
//        String sex = req.getParameter("sex");//性别
//        String country = req.getParameter("country");//国籍
//        String idtype = req.getParameter("idtype");//证件类型
//        String idnumber = req.getParameter("idnumber");//证件号码
//        String phone = req.getParameter("phone");//电话号码
//        String address = req.getParameter("address");//联系地址
//        req.getSession().getAttribute()
        //创建model层对象
//        User user1 = new User();
//        user1.setUid(2);
//        user1.setSex(sex);
//        user1.setCountry(country);
//        user1.setIdtype(idtype);
//        user1.setIdnumber(idnumber);
//        user1.setPhone(phone);
//        user1.setAddress(address);
        User user1 = (User) req.getSession().getAttribute("user");
        //调用业务层
//        User user = userService.select(user1);
        if(user1!=null){
            //不为空，保存到域对象session
            req.getSession().setAttribute("user",user1);
            resp.sendRedirect("/My/MyInformation.jsp");
        }else{
            //select为空
            req.setAttribute("msg","该查询对象不存在");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }
    /**
     * 点击【修改】按钮调用的方法
     * update
     * */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        //获取参数
        String name = req.getParameter("name");//姓名
        String sex = req.getParameter("sex");//性别
        String country = req.getParameter("country");//国籍
        String idtype = req.getParameter("idtype");//证件类型
        String idnumber = req.getParameter("idnumber");//证件号码
        String phone = req.getParameter("phone");//电话号码
        String address = req.getParameter("address");//联系地址
        //创建model层对象
        User user1 = (User) req.getSession().getAttribute("user");
        user1.setName(name);
        user1.setSex(sex);
        user1.setCountry(country);
        user1.setIdtype(idtype);
        user1.setIdnumber(idnumber);
        user1.setPhone(phone);
        user1.setAddress(address);

        //调用业务层
        int row = userService.updateUserMsg(user1);
        if(row>0){
            //不为空，保存到域对象session
            req.getSession().setAttribute("user",user1);
            resp.sendRedirect("/My/MyInformation.jsp");
        }
//        else{
//            //select为空
//            req.setAttribute("msg","该查询对象不存在");
//            req.getRequestDispatcher("login.jsp").forward(req,resp);
//        }
    }
}
